<template>
    <div>

        <h3>用户管理</h3>
        <el-table
                :data="tableData.slice((currentPage-1)*PageSize,currentPage*PageSize)"
                style="width: 100%">
            <el-table-column
                    label="用户名"
                    prop="username">
            </el-table-column>
            <el-table-column
                    label="密码"
                    prop="userpwd">
            </el-table-column>
            <el-table-column
                    label="角色"
                    prop="usersole">
            </el-table-column>
            <el-table-column
                    label="真实姓名"
                    prop="usertruename">
            </el-table-column>
            <el-table-column
                    align="right">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="change(scope.row.username)">修改
                    </el-button>
                    <el-button
                            size="mini"
                            @click="del(scope.row.userId)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="tabListPage">
            <el-pagination @size-change="handleSizeChange"
                           @current-change="handleCurrentChange"
                           :current-page="currentPage"
                           :page-sizes="pageSizes"
                           :page-size="PageSize" layout="total, sizes, prev, pager, next, jumper"
                           :total="totalCount">
            </el-pagination>
        </div>

        <el-button type="primary" @click="addUser" style="margin: 20px;">添加</el-button>

        <div style="width:600px; height: 200px; border: 2px solid;">
            <span>--查询用户信息--</span>
            <el-form ref="userForm">
                <el-row>
                    <el-form-item label="用户名称：" prop="userName">
                        <el-input v-model="userName"></el-input>
                    </el-form-item>
                </el-row>
                <el-button @click="findByName('userForm')">查询</el-button>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "UserView",


        data() {
            return {
                userName: "",
                userForm:"",
                totalCount:null,
                // 默认显示第几页
                currentPage:1,
                // 个数选择器（可修改）
                pageSizes:[1,2,3,4],
                // 默认每页显示的条数（可修改）
                PageSize:3,

                tableData: [],
                search: ''

            }
        },
        mounted() {
            this.findByName("")
        },
        methods: {
            del(id) {
                this.$axios.get("api/sysuser/del?id="+id).then(
                    resp=>{
                        if(resp.data==true){
                            this.findByName("");
                        }
                        else {
                            this.$message.error("删除失败")
                        }
                    }
                )
            },
            change(username) {
                this.$router.push("changeGrade",username)
            },
            addUser() {
                this.$router.push("addUser")
            },

            findByName() {
                this.$axios.get("api/sysuser/findByName?name="+this.userName).then(r =>{
                    console.log(r.data)
                   this.tableData = r.data;
                })

            },

            handleSizeChange(size) {
                // 改变每页显示的条数
                this.PageSize=size
                // 注意：在改变每页显示的条数时，要将页码显示到第一页
                this.currentPage=1
            },
            // 显示第几页
            handleCurrentChange(size) {
                // 改变默认的页数
                this.currentPage=size
            },
        },
    }
</script>

<style scoped>

</style>